package com.ruoyi.web.controller.dongfang;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.Folder;
import com.ruoyi.question.service.IFolderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 目录 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-10-11
 */
@Controller
@RequestMapping("/question/folder")
public class FolderController extends BaseController
{
    private String prefix = "question/folder";
	
	@Autowired
	private IFolderService folderService;
	
	@RequiresPermissions("question:folder:view")
	@GetMapping()
	public String folder()
	{
	    return prefix + "/folder";
	}
	
	/**
	 * 查询目录列表
	 */
	@RequiresPermissions("question:folder:list")
	@GetMapping("/list")
	@ResponseBody
	public List<Folder> list(Folder folder)
	{
        List<Folder> list = folderService.selectFolderList(folder);
		return list;
	}
	
	
	/**
	 * 导出目录列表
	 */
	@RequiresPermissions("question:folder:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Folder folder)
    {
    	List<Folder> list = folderService.selectFolderList(folder);
        ExcelUtil<Folder> util = new ExcelUtil<Folder>(Folder.class);
        return util.exportExcel(list, "folder");
    }
	
	/**
	 * 新增目录
	 */
	@GetMapping("/add/{id}")
	public String add(@PathVariable("id") Long id, ModelMap mmap)
	{
		mmap.put("folder",folderService.selectFolderById(id));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存目录
	 */
	@RequiresPermissions("question:folder:add")
	@Log(title = "目录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Folder folder)
	{		
		return toAjax(folderService.insertFolder(folder));
	}

	/**
	 * 修改目录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		Folder folder = folderService.selectFolderById(id);
		if (StringUtils.isNotNull(folder) && 100L == id)
		{
			folder.setParentName("无");
		}
		mmap.put("folder", folder);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存目录
	 */
	@RequiresPermissions("question:folder:edit")
	@Log(title = "目录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Folder folder)
	{		
		return toAjax(folderService.updateFolder(folder));
	}
	
	/**
	 * 删除目录
	 */
	@RequiresPermissions("question:folder:remove")
	@Log(title = "目录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(HttpServletRequest httpServletRequest)
	{
		String ids = httpServletRequest.getParameter("id");
		return toAjax(folderService.deleteFolderByIds(ids));
	}

	/**
	 * 选择部门树
	 */
	@GetMapping("/selectDeptTree/{id}")
	public String selectDeptTree(@PathVariable("id") Long id, ModelMap mmap)
	{
		mmap.put("folder", folderService.selectFolderById(id));
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData()
	{
		List<Ztree> ztrees = folderService.selectFolderTree(new Folder());
		return ztrees;
	}
}
